package dp.mobile.comm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import android.util.Log;
 
public class RestClient {
 
    private static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
 
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
    
    public static InputStream getResponseInputStream(String pURL) {
    	HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(pURL); 
 
        HttpResponse response;
        
        try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            
            if (entity != null) {
                InputStream instream = entity.getContent();
                return instream;
                
                //String result = convertStreamToString(instream);
                
                //return result;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return null;
    }
 
    public static JSONObject get(String url)
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url); 
 
        HttpResponse response;
        JSONObject json = null;
        
        try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            
            if (entity != null) {
                InputStream instream = entity.getContent();
                
                String result= convertStreamToString(instream);
                //Log.i("this" , result);
                json=new JSONObject(result);
                instream.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        return json;
    }
    
    public static HttpResponse post(String url, JSONObject json){
    	HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
    	HttpResponse response;
    	
        try {
        	List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("json", json.toString()));
            Log.i("Rest Client", json.toString());
            
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            
            response = httpclient.execute(httppost);
            
            /** debug response */
            
            HttpEntity entity = response.getEntity();
            if (entity != null) {
 
                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                Log.i("RestClient loh",result);
                instream.close();
            }
            return response;
        } catch (UnsupportedEncodingException uEx){
        	
        } catch (IOException iEx){
        	Log.d("RestClient", "IO Error = " + iEx);
        }
        
        return null;
    }
 
}
